package zhihui.mobile.presenter.myself;

import android.content.Context;

import com.zgm.utils.SharedPreferencesUtis;

import org.json.JSONObject;

import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.myself.IUpdateNameContract;
import zhihui.mobile.ui.myself.IUpdateNameContract.IUpdateNamePresenter;

/**
 * Created by 张公铭 on 2017/9/24 0024.
 */

public class UpdateNamePresenter implements IUpdateNamePresenter
{
    private Context mContext;
    private IUpdateNameContract.IUpdateNameView nameView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        nameView = (IUpdateNameContract.IUpdateNameView) view;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void saveUserName(final String name)
    {
        nameView.showLoading();
        UserManager.getInstance().updateUserInfo(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                nameView.hindLoading();
                nameView.showMessage("修改成功");
                UserManager.getInstance().getCurUser().setName(name);
                SharedPreferencesUtis.saveSharedPreference(mContext, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE, UserManager.getInstance().getCurUser());
            }

            @Override
            public void failed(String message)
            {
                nameView.hindLoading();
                nameView.showMessage(message);
            }
        }, name, UserManager.getInstance().getCurUser().getSex());
    }
}
